<?php

class GrupoController extends TWin8 {

    private $logic;

    public function __construct() {
        parent::__construct();
        $this->logic = new GrupoLogic();
    }

    public function index() {
        $this->REDIRECT->goToAction('listar');
    }

    public function listar() {
        
        $this->addDados('toolbar', TWin8Helper::displayToolBar(array('novo')));
        
        $arrayObjGrupo = $this->logic->listar(null,null,true);
        $this->addDados('listGrupos', $arrayObjGrupo);
        $this->TPageSecondary('lista');
        
    }
    
     # Ajax
    public function listarProblemaPorGrupo() {

        $id = $this->getParam("id");

        # Lista de Grupo
        $ArrayObjGrupo = $this->logic->listar("ide_grupo = {$id}","descricao");

        if ($ArrayObjGrupo != null) {
            foreach ($ArrayObjGrupo as $objGrupo) {
                $arrayJson[] = array('id' => $objGrupo->getId(), 'descricao' => utf8_encode($objGrupo->getDescricao()));
            }
        } else {
            $arrayJson = false;
        }


        echo json_encode($arrayJson);
        
    }
    
    public function cadastrar(){
        
        $this->HTML->addJavaScript(PATH_JS_CORE_URL . "jquery.validate.js");
        $this->HTML->addJavaScript(PATH_JS_CORE_URL . "mask.js");
        $this->HTML->addJavaScript(PATH_JS_URL . $this->_controller . "/" . $this->_action . ".js");
        
        $objLogicSetor = new SetorLogic();
        $this->addDados("listSetor", TFormHelper::optionSelect($objLogicSetor->listar(null,"nome")));
        
        $this->TPageSecondary('cadastro');
    }
    
    
     public function inserir() {
        
        $salvar = $this->logic->salvar($_POST, false);
        $this->REDIRECT->goToAction('listar');
        
    }
    
    public function informar() {

        if (!$this->isParam('id')) {
            $this->REDIRECT->goToAction("listar");
        }

        $this->HTML->addJavaScript(PATH_JS_CORE_URL . "MetroUI/pagecontrol.js");
        $this->addDados('toolbar', TWin8Helper::displayToolBar(array('editar')));
        $this->addDados("objGrupo", $this->logic->obterPorId($this->getParam('id'), true));

        $this->TPageSecondary('informa');
    }

     public function editar() {
        
        $this->HTML->addJavaScript(PATH_JS_CORE_URL . "jquery.validate.js");
        $this->HTML->addJavaScript(PATH_JS_CORE_URL . "jquery.populate.js");
        $this->HTML->addJavaScript(PATH_JS_URL . $this->_controller . "/" . $this->_action . ".js");
        
        $setorLogic = new SetorLogic();
        $this->addDados("listSetor", TFormHelper::optionSelect($setorLogic->listar(null, "nome")));
        
        $this->addDados('json_objeto', $this->logic->objectToJson( $this->logic->obterPorId($this->getParam('id')) ));

        $this->TPageSecondary('edita');
    }
    
    public function atualizar() {

        $salvar = $this->logic->salvar($_POST, TRUE);

        if ($salvar[0]) {
            $this->REDIRECT->setUrlParameter("id", $_POST['id']);
            $this->REDIRECT->goToAction('informar');
        } else {
            #Retornar erros;
        }
    }
    
   
    
}

?>